﻿using Simple.ISysRepository;
using Simple.Tools;
using Simple.Tools.SnowFlake;
using SimpleSaas.Common;
using SimpleSaas.Common.DataBase;
using SimpleSaas.Entity;
using SimpleSaas.Entity.Entity;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Simple.SysRepository
/// <summary>
/// 版 本 钢易智企2.0 V 2.0.0 钢易智企2.0
/// Copyright (c) 2023 杭州钢易科技有限公司
/// 创建人：曾东明
/// 日 期：2024
/// 描 述：
/// </summary>
{
	public class SysAppRepository : WholeRepository<SysAppEntity, long>, ISysAppRepository
	{
		[Autowired]

		protected ISysAppTenantRepository sysAppTenantRepository { get; set; }

		/// <summary>
		/// 创建企业绑定app
		/// </summary>
		/// <param name="appIds"></param>
		/// <param name="tenantId"></param>
		public async Task BatchCreateBindAsync(List<string> appIds, long tenantId)
		{
			List<SysAppTenantRelationEntity> relations = new List<SysAppTenantRelationEntity>();
			if (SimpleCheck.IsNotNullOrEmpty(appIds))
			{
				List<long> Ids = appIds.Select(app => app.ToInt64()).ToList();
				Ids = await this.Where(sysApp => Ids.Contains(sysApp.Id) && !sysApp.IsDeleted).Select(sysApp => sysApp.Id).ToListAsync();
				if (SimpleCheck.IsNotNullOrEmpty(appIds))
				{
					foreach (var appId in Ids)
					{
						SysAppTenantRelationEntity relation = new SysAppTenantRelationEntity();
						relation.AppId = appId;
						relation.TenantId = tenantId;
						relations.Add(relation);
					}
				}
			}
			await sysAppTenantRepository.CreateAsync(relations);
		}

		/// <summary>
		/// 更新企业绑定app
		/// </summary>
		/// <param name="appIds"></param>
		/// <param name="tenantId"></param>
		public async Task BatchUpdateBindAsync(List<string> appIds, long tenantId)
		{
			List<SysAppTenantRelationEntity> relations = new List<SysAppTenantRelationEntity>();
			List<SysAppTenantRelationEntity> updateRelations = new List<SysAppTenantRelationEntity>();
			if (SimpleCheck.IsNotNullOrEmpty(appIds))
			{
				List<long> Ids = appIds.Select(app => app.ToInt64()).ToList();
				Ids = await this.Where(sysApp => Ids.Contains(sysApp.Id) && !sysApp.IsDeleted).Select(sysApp => sysApp.Id).ToListAsync();

				var oldList=await sysAppTenantRepository.Where(r=>r.TenantId.Equals(tenantId)).ToListAsync();

				if (SimpleCheck.IsNotNullOrEmpty(appIds))
				{
					
					foreach (var appId in Ids)
					{
						if (!oldList.Any(r=>r.AppId.Equals(appId)))
						{
							SysAppTenantRelationEntity relation = new SysAppTenantRelationEntity();
							relation.AppId = appId;
							relation.TenantId = tenantId;
							relations.Add(relation);
						}
						else
						{
							SysAppTenantRelationEntity relation=oldList.FirstOrDefault(r=>r.AppId.Equals(appId));
							relation.IsDeleted = false;
							updateRelations.Add(relation);
						}
					}
				}
				List<long> deleteIds = oldList.Where(app=>!Ids.Contains(app.AppId)).Select(app=>app.AppId).ToList();

				if (SimpleCheck.IsNotNullOrEmpty(deleteIds))
				{
					await sysAppTenantRepository.SoftDeleteAsync(r => deleteIds.Contains(r.AppId) && r.TenantId.Equals(tenantId));
				}
			}

			await sysAppTenantRepository.CreateAsync(relations);

			await sysAppTenantRepository.UpdateAsync(updateRelations);
		}

		/// <summary>
		/// 
		/// </summary>
		/// <param name="matchCondition"></param>
		/// <returns></returns>
		public ISugarQueryable<SysAppView> Query(Action<ISugarQueryable<SysAppEntity>> matchCondition)
		{
			SimpleCheck.IsNotNull(matchCondition);

			var query=dbClient.Queryable<SysAppEntity>();

			matchCondition.Invoke(query);

			return query.Select(app=>new SysAppView()
			{
				Id=app.Id.ToString(),
				AppName=app.AppName,
				Sort=app.Sort,
				CreationTime=app.CreationTime,
				Remark=app.Remark,
			});
		}
	}
}
